package com.zbg;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author Zoe.Zhang
 * @Date: 2021/07/03
 * @Description $description
 */
@WebFilter(urlPatterns = "/servlet1/**")
public class MyFilter implements Filter {
    public void destroy() {
        System.out.println("filter destroy");

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        Object user = req.getAttribute("user");
        if (Objects.equals(null,user)){
            resp.getWriter().write("loginfirst");
        }else {
            chain.doFilter(req, resp);
        }
    }

    public void init(FilterConfig config) throws ServletException {
        System.out.println("filter init");
    }

}
